import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ChatClient extends Frame {
    Socket s = null;
    DataOutputStream dos = null;
    DataInputStream  dis = null;
    private boolean bConnected = false;

    TextField tfTxt = new TextField();

    TextArea taContent = new TextArea();

    public static void main(String[] args) {
        new ChatClient().launchFrame();
    }

    public void launchFrame() {
        setLocation(400, 300);// 窗口位置
        this.setSize(300, 300);// 大小
        add(tfTxt, BorderLayout.SOUTH);// 布局borderlayout
        add(taContent, BorderLayout.NORTH);
        pack();// 删除空白处
        // 点击错号后关闭窗口
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(0);
            }
        });
        tfTxt.addActionListener(new TFListener());
        setVisible(true);// 使窗口显示
        connect();

        new Thread(new RecvThread()).start();
    }

    public void connect() {
        try {
            s = new Socket("127.0.0.1", 8888);
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());
            bConnected = true;
System.out.println("connected!");
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void disconnect(){
        try {
            dos.close();
            s.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private class TFListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String str = tfTxt.getText().trim();//拿到字符串，trim去除两边的空格
            taContent.setText(str);
            tfTxt.setText("");//回车后将输入内容转为空字符串
            try {
                dos.writeUTF(str);
                dos.flush();
                //dos.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }
    
    private class RecvThread implements Runnable{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                while (bConnected) {
                    String str = dis.readUTF();
                    System.out.println(str);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}